package com.example.tetstmybatis.controller;
import com.example.tetstmybatis.domain.User2;
import com.example.tetstmybatis.domain.User_sb;
import com.example.tetstmybatis.req.RepUser_sb;
import com.example.tetstmybatis.resp.CommonResp;
import com.example.tetstmybatis.service.User_sbService;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@RestController
public class User_sbController {
    @Resource
    private User_sbService user_sbService;
    @GetMapping("/rzall")
    public CommonResp rzall(RepUser_sb req){
        CommonResp list = user_sbService.rzall(req);
        return  list;
    }
    @PostMapping("/add")
    public CommonResp add(@RequestBody RepUser_sb req){
        CommonResp resp = new CommonResp<>();
        User_sb user_sb = user_sbService.find(req.getWxName());
        if(ObjectUtils.isEmpty(user_sb)){
            //该注册名不存在
            boolean register = user_sbService.add(req);
            if(register){
                //添加成功
                resp.setSuccess(true);
            }else {
                //添加失败;
                resp.setSuccess(false);
            }

        }else {
            //该注册用户名已存在9
            resp.setSuccess(false);
        }
        return resp;
    }

    @PostMapping("/update")
    public CommonResp update(@RequestBody User_sb req){
        CommonResp resp = new CommonResp<>();
        User_sb user_sb = user_sbService.find(req.getWxName());
        if(ObjectUtils.isEmpty(user_sb)){
            resp.setSuccess(false);
        }else {
            //找微信名
            boolean register = user_sbService.update(user_sb);
            if(register){
                //修改成功
                resp.setSuccess(true);
            }else {
                //修改失败;
                resp.setSuccess(false);
            }
        }
        return resp;
    }

    @PostMapping("/publish")
    public CommonResp publish(){
        CommonResp resp = new CommonResp<>();
        resp.setSuccess(user_sbService.publish());
        return resp;
    }

    @PostMapping("/read")
    public CommonResp read(@RequestBody User_sb req){
        CommonResp resp = new CommonResp<>();
        System.out.println(req.getWxName());
        String file=req.getWxName();
        try {
            String[] args1 = new String[] { "C:\\Users\\twy\\anaconda3\\python.exe","C:\\Users\\twy\\PycharmProjects\\房产局\\read_card.py", String.valueOf(file)};
            Process proc = Runtime.getRuntime().exec(args1);// 执行py文件
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
            proc.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
            resp.setSuccess(false);
        } catch (InterruptedException e) {
            e.printStackTrace();
            resp.setSuccess(false);
        } catch (IllegalStateException e) {
        e.printStackTrace();
        resp.setSuccess(false);
    }
        return resp;
    }

}
